package com.medical.lsq.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.medical.lsq.dao.SysMenuMapper;
import com.medical.lsq.entity.SysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

@Service
@Transactional

public class MenuService {


    @Autowired
    private SysMenuMapper menuMapper;
    /**
     * 添加限权
     * @param menu
     */
    public void add(SysMenu menu){

        menu.setCreateTime(new Timestamp(System.currentTimeMillis()));
        menu.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        menuMapper.insert(menu);
    }


    /**
     * 删除权限
     * @param id
     */
    public void delete(String id) {
        if (id != null) {
            menuMapper.deleteByPrimaryKey(id);
        }
    }

/**
 * 修改区域
 * @param menu
 */
        public void edit(SysMenu menu){
            menu.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            menuMapper.updateByPrimaryKeySelective(menu);
        }


    /**
     * 根据ID主键查找区域信息
     * @param id
     * @return
     */
    public SysMenu getMenuById(String id){return menuMapper.selectByPrimaryKey(id);}
    /**
     * 分页查找
     * @param menuName
     * @param currentPage
     * @param limit
     * @return
     */
    public PageInfo<SysMenu> getMenuList(String menuName, Integer currentPage, Integer limit) {
        //利用PageHelper分页查询 注意：这个一定要放在查询语句的第一行，否者无法进行分页，因为他对金随后的第一个sql语句
        PageHelper.startPage(currentPage,limit);
        List<SysMenu> list = menuMapper.getMenuList(menuName);
        PageInfo<SysMenu> pageInfo = new PageInfo<SysMenu>(list);
        return pageInfo;
    }
    public List<SysMenu> getMenuTree(String menuName) {
        return menuMapper.getMenuList(menuName);
    }

}